//
//  ImageWaterVC.m
//  UILbrary
//
//  Created by Apple on 2022/6/29.
//  Copyright © 2022 mac. All rights reserved.
//

#import "ImageWaterVC.h"

@interface ImageWaterVC ()

@end

@implementation ImageWaterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    UIImage *image = [UIImage imageNamed:@"1"];
    UIImageView *imgView = [UIImageView new];
    imgView.frame = self.view.bounds;
    [self.view addSubview:imgView];
    
    NSDictionary *dic = @{
        NSFontAttributeName:[UIFont systemFontOfSize:20],
        NSForegroundColorAttributeName:[UIColor colorWithRed:255/255.0 green:0 blue:0 alpha:0.3]
    };
    imgView.image = [self waterImageWithImage:image text:@"广东省深圳市福田区某某街道某某小区" textPoint:CGPointMake(50, 100) attributedString:dic];
}

// 给图片添加文字水印：
- (UIImage *)waterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed{
    //1.开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.绘制图片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //添加水印文字
    [text drawAtPoint:point withAttributes:attributed];
    //3.从上下文中获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4.关闭图形上下文
    UIGraphicsEndImageContext();
    //返回图片
    return newImage;
}

// 给图片添加文字水印：
- (UIImage *)waterImageWithImage_1:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed{
    //1.开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.绘制图片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //添加水印文字
    [text drawAtPoint:point withAttributes:attributed];
    //3.从上下文中获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4.关闭图形上下文
    UIGraphicsEndImageContext();
    //返回图片
    return newImage;
}




@end
